package com.dycx.system.converter;

import java.time.LocalDateTime;

import cn.hutool.core.bean.BeanUtil;
import com.dycx.system.enums.RegionLevelEnum;
import lombok.Data;

import java.io.Serializable;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.system.entity.RegionDO;
import com.dycx.system.vo.RegionVO;
import com.dycx.system.dto.RegionDTO;

/**
 * (Region)实体转换类
 *
 * @author makejava
 * @since 2023-10-31 23:38:11
 */


@Data
public class RegionConverter implements Serializable {
    public static RegionDO dtoToEntity(RegionDTO dto) {
        if (dto == null) return null;
        RegionDO entity = BeanUtil.toBean(dto, RegionDO.class);
        return entity;
    }

    public static RegionVO entityToVo(RegionDO entity) {
        if (entity == null) return null;
        RegionVO vo = BeanUtil.toBean(entity, RegionVO.class);

        vo.setHasChildren(!vo.getLevel().equals(RegionLevelEnum.STREET.getCode()));

        vo.setLevelName(RegionLevelEnum.converter(vo.getLevel()));

        return vo;
    }

    public static List<RegionVO> entityToVoList(List<RegionDO> entityList) {
        List<RegionVO> voList = new ArrayList<>();
        for (RegionDO entity : entityList) {
            voList.add(entityToVo(entity));
        }
        return voList;
    }

    public static Page<RegionVO> entityToVoPage(Page<RegionDO> page) {

        List<RegionVO> newRecords = new ArrayList<>();

        List<RegionDO> records = page.getRecords();
        for (RegionDO entity : records) {
            newRecords.add(entityToVo(entity));
        }

        Page<RegionVO> voPage = new Page<>();
        voPage.setRecords(newRecords);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());

        return voPage;
    }

}
